package com.fitnessapp.main;

import java.util.List;
import java.util.Map;

import com.fitnessapp.database.DatabaseStrings;
import com.fitnessapp.database.MySqlHelper;
import com.fitnessapp.database.RemoteStorageConnection;


import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.method.ScrollingMovementMethod;
import android.view.ContextMenu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupMenu;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;

public class ExerciseDisplay extends Activity {
	public static final String EXERCISE_PRIMARY_KEY = "exercise_id";
	
	private String exerciseId;
	private ProgressDialog progress;
	private ImageView exerciseImg;
	private TextView exerciseDesc;
	private TextView exerciseName;
	
	private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
//        	Exercise exercise = (Exercise)msg.obj;
//        
//            exerciseImg.setImageBitmap(exercise.getMedia());
//            exerciseDesc.setText(exercise.getDescription());
//            exerciseName.setText(exercise.getName());        
//            exerciseDesc.setMovementMethod(new ScrollingMovementMethod());
        }
	};
	
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		
	//	setContentView(R.layout.activity_exercise_display);
		setupTabs();
		//get the exercise id
		exerciseId = getIntent().getStringExtra(EXERCISE_PRIMARY_KEY);		
				
		progress = ProgressDialog.show(this, "", "Searching...", true);	
		
		//exerciseImg = (ImageView) findViewById(R.id.exerciseImg);
		//exerciseDesc = (TextView) findViewById(R.id.exerciseDesc);
		//exerciseName = (TextView) findViewById(R.id.exerciseName);
		
		//Button addExercise = (Button)findViewById(R.id.addExercise);
		//addExercise.setOnClickListener(new OnClickListener() {
			
//			public void onClick(View v) {
//				 
//				Dialog routineDialog = new Dialog(ExerciseDisplay.this);
//			//	routineDialog.setContentView(R.layout.dialog_list_layout);
//				routineDialog.setTitle("Select a Routine");
//			//	ListView list = (ListView)routineDialog.findViewById(R.id.dialog_list_view);
//				
//				
//				String[] data = new String[]{"routine1", "routine2", "routine3", "routine4", "routine5", "routine6"};
//			//	list.setAdapter(new ArrayAdapter<String>(ExerciseDisplay.this, R.layout.list_view_text_row, R.id.rowText, data));
//				routineDialog.show();
//				
//			}
//		});
		
		(new QueryThread(this, handler, progress,
				"exercise_name, exercise_media1, exercise_desc",
				"exercises",
				"exercise_id = \'" + exerciseId +"\'")).start();	
		
	}
	
	private void setupTabs(){
//		TabHost tabs = (TabHost)findViewById(R.id.tabhost);
//		tabs.setup();
//		TabSpec specs = tabs.newTabSpec("tag1");
//		specs.setContent(R.id.description);
//		specs.setIndicator("Description");
//		tabs.addTab(specs);
//		
//		specs = tabs.newTabSpec("tag2");
//		specs.setContent(R.id.comments);
//		specs.setIndicator("Comments");
//		tabs.addTab(specs);
//		
//		specs = tabs.newTabSpec("tag3");
//		specs.setContent(R.id.history);
//		specs.setIndicator("History");
//		tabs.addTab(specs);
	}
	
	private class QueryThread extends Thread {
		private Context context;
		private Handler handler;
		private ProgressDialog progress;
		private String select;
		private String from;
		private String where;
		
		public QueryThread(Context context, Handler handler, ProgressDialog progress,
				String select, String from, String where){
			this.context = context;
			this.handler = handler;
			this.select = select;
			this.from = from;
			this.where = where;
			this.progress = progress;
		}
		
		public void run(){
			
			MySqlHelper dbHelper = new MySqlHelper(DatabaseStrings.DB_URL);
			List<Map<String,String>> result = dbHelper.query(select, from, where);			
		
			if( result.size() > 0 ){
				Map<String,String> exerciseData = result.get(0);
				
//				Exercise exercise = new Exercise(exerciseId);
//				exercise.load(dbHelper);
//				Message msg = new Message();
//				msg.obj = exercise;
//	
//				handler.sendMessage(msg);
			}
			
			progress.dismiss();
		}
	}


}
